# -*- coding: utf-8 -*-
#这是个测试wsgi

from cgi import parse

def application(environ, start_response):
    '''
    apache wsgi模块，会调用这个函数，来处理每一个请求
    '''

    #从environ里面解析出http参数来
    params = parse(None, environ)
    
    #假设http请求里面一定有两个键为name和age的参数
    #获取name,age参数的值：
    if len(params) > 0:
        if params.get('name') != None and params.get('age') != None:
            nameparam = params['name']
            ageparam = params['age']
            return response(environ, start_response, 'name is %s\n age is %s' % (nameparam, ageparam))
        else:
            return response(environ, start_response, 'no name and age found')
    else:
        return response(environ, start_response, 'no name and age found')


def response(environ, start_response, msg):
    '''
    对请求做一个文本格式的应答，msg可以是一个json文本
    '''
    status = '200 ok'
    output = msg
    
    response_headers = [('Content-type', 'text/plain'),
            ('Content-Length', str(len(output)))]
    
    start_response(status, response_headers)
    
    return [output]  